﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ExhibitionDraw.Control;
using ExhibitionDraw.Server;
using ExhibitionDraw.Service;
using System.ServiceModel.DomainServices.Client;
using ExhibitionDraw.Common;
namespace ExhibitionDraw
{
    public partial class MainPage : UserControl
    {
        ObservableCollection<Service.FloorModel> floors;
        ObservableCollection<Service.PavilionModel> pavilions;

        Service.CommServiceClient service = null;
        DPaint dp = null;
        public MainPage()
        {

            InitializeComponent();
            // service = ServiceHelper.InitService();

            //MainPage_LayoutUpdated();
            // this.LayoutUpdated += new EventHandler(MainPage_LayoutUpdated);

            InitData();


        }

        #region 初始化页面
        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void InitData()
        {
            dp = new DPaint();

            service = ServiceHelper.InitService();
            Scroll.Width = App.Current.Host.Content.ActualWidth;
            Scroll.Height = App.Current.Host.Content.ActualHeight - 80;
            Scroll.Margin = new Thickness(0, 80, 0, 0);
            //Img.Source = new BitmapImage(new Uri("/ExhibitionDraw;component/Images/1.jpg",UriKind.Relative));
            service.GetPavilionListAsync();
            service.GetPavilionListCompleted += new EventHandler<Service.GetPavilionListCompletedEventArgs>(GetPavilionListCompleted);

        }
        private void GetPavilionListCompleted(object sender, Service.GetPavilionListCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
            {
                pavilions = (ObservableCollection<Service.PavilionModel>)e.Result;

                cboxZhan.DisplayMemberPath = "PaName";
                cboxZhan.ItemsSource = pavilions;
                cboxZhan.SelectedIndex = 0;
            }
        }
        #endregion

        /// <summary>
        /// 规则图形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DRectangle_DRectangleEndDrawed(object sender, DrawEventArgs e)
        {


            //FrameworkElement element = sender as FrameworkElement;
            //element.ReleaseMouseCapture();
            var rg = sender as Rectangle;
            rg.ReleaseMouseCapture();
            //var txt=new TextBlock();
            //txt.Text = "F0001";
            //txt.TextWrapping = TextWrapping.Wrap;
            //txt.

            //MessageBox.Show(rg.Width.ToString());
            //MessageBox.Show(rg.Height.ToString());
            var p = e.Points.FirstOrDefault();
            Point second = new Point(p.X + rg.Width, p.Y);
            Point three = new Point(p.X, p.Y + rg.Height);
            Point four = new Point(p.X + rg.Width, p.Y + rg.Height);
            //txt.Margin = new Thickness(p.X, p.Y , 0, 0);

            //master.Children.Add(txt);

            var obj = cboxZhan.SelectedItem as Service.PavilionModel;
            var fobj = cbox.SelectedItem as Service.FloorModel;
            StandChild cld = new StandChild();
            if (obj != null)
            {
                cld.PavID = Convert.ToInt32(obj.Id);
            }
            if (fobj != null)
            {
                cld.FloorID = Convert.ToInt32(fobj.Id);
            }
            cld.strPoint = p.X + "," + p.Y + ";" + second.X + "," + second.Y + ";" + three.X + "," + three.Y + ";" + four.X + "," + four.Y + ";";
            cld.txtpoint.Text = cld.strPoint;
            cld.Show();

            //string s = "";
            //foreach (Point p in e.Points)
            //{
            //    s += string.Format("{0},{1}; ", p.X, p.Y);
            //}





        }

        /// <summary>
        /// 不规则图形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DPolyLine_PolylineEndDrawed(object sender, DrawEventArgs e)
        {

            // MessageBox.Show(  e.CurrentPoint.X.ToString()); 
            //var dp=sender as DPolygon;
            
            var dp = sender as DPolygon;
           
            //FrameworkElement element = sender as FrameworkElement;
            //element.ReleaseMouseCapture();

            dp.ReleaseMouseCapture();

            //rg.MouseLeftButtonDown -= new MouseButtonEventHandler(DPolygon.MouseLeftButtonDownEvent);
           // rg.PointDrawed -= new EventHandler<DrawEventArgs>(DPolyLine_PolylineEndDrawed);
            StandChild cld = new StandChild();

            cld.Show();

        }

        void rg_PointDrawed(object sender, DrawEventArgs e)
        {
          
        }

        #region 不规则展位
        /// <summary>
        /// 不规则展位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Un_Click(object sender, RoutedEventArgs e)
        {
            Draw(1);
            //if (master.Children.Contains(dp))
            //{
            //    master.Children.Remove(dp);
            //}

            //dp.dtp = 1;

            //master.Children.Add(dp);

            btn_Position.IsEnabled = true;
            btn_Un.IsEnabled = false;
        }
        #endregion
        /// <summary>
        /// 规则展位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Position_Click(object sender, RoutedEventArgs e)
        {

            //if (master.Children.Contains(dp))
            //{
            //    master.Children.Remove(dp);
            //}
            //dp.dtp = 2;

            //master.Children.Add(dp);
            Draw(2);
            btn_Un.IsEnabled = true;
            btn_Position.IsEnabled = false;
        }


        public void Draw(int tp)
        {
            // var p ;
            switch (tp)
            {
                case 1:
                    DPolygon p = new DPolygon();
                    p.PolylineEndDrawed += new EventHandler<DrawEventArgs>(DPolyLine_PolylineEndDrawed);
                    master.Children.Add(p);


                    break;
                case 2:
                    DRectangle pr = new DRectangle();
                    pr.DRectangleEndDrawed += new EventHandler<DrawEventArgs>(DRectangle_DRectangleEndDrawed);
                    master.Children.Add(pr);

                    break;
            }
        }


        #region 展馆和楼层选择
        private void cboxZhan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PavilionModel obj = ((ComboBox)sender).SelectedItem as PavilionModel;
            var selID = Convert.ToInt32(obj.Id);
            service.GetFloorByPIdAsync(selID);
            service.GetFloorByPIdCompleted += new EventHandler<GetFloorByPIdCompletedEventArgs>(GetFloorByPIdCompleted);


        }

        private void GetFloorByPIdCompleted(object sender, GetFloorByPIdCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                floors = e.Result;
                cbox.DisplayMemberPath = "FloorName";
                cbox.ItemsSource = floors;
                cbox.SelectedIndex = 0;
            }
        }

        private void cbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FloorModel obj = ((ComboBox)sender).SelectedItem as FloorModel;
            if (obj != null)
            {
                try
                {
                    Img.Source = new BitmapImage(new Uri(Server.ServiceHelper.GetUrl() + obj.FloorPath));

                    // CreatMenu();
                }
                catch (Exception ex)
                {
                    //throw ex;
                }
            }
        }
        #endregion


        private void sliderImg_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //if(e.OldValue)


            if (e.NewValue > e.OldValue)
            {
                //    //Scroll.Width += e.NewValue + 100;
                //    //Scroll.Height += e.NewValue + 100;
                //    master.Width += 10;
                //    master.Height += 10;
                if (master.Width < 1200)
                {
                    master.Width += 10;
                    master.Height += 10;
                    Img.Width += 10;
                    Img.Height += 10;
                }
            }
            else
            {
                //Scroll.Width += e.NewValue - 100;
                //    //Scroll.Height += e.NewValue - 100;
                if (master.Width > 700)
                {
                    master.Width += -10;
                    master.Height += -10;
                    Img.Width -= 10;
                    Img.Height -= 10;
                }
                //if (e.OldValue != 0)
                //{
                //    Img.Width = Img.Width / e.OldValue;
                //    Img.Height = Img.Height / e.OldValue;
                //}
            }
        }

        private void btnSetColor_Click(object sender, RoutedEventArgs e)
        {
            ColorChild cld = new ColorChild();
            cld.Show();

        }



    }
}
